package com.cc.library.action.app;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cc.library.domain.Admin;
import com.cc.library.domain.Authorization;
import com.cc.library.domain.BackInfo;
import com.cc.library.domain.Book;
import com.cc.library.domain.BookType;
import com.cc.library.domain.Collection;
import com.cc.library.domain.ForfeitInfo;
import com.cc.library.domain.PageBean;
import com.cc.library.domain.Reader;
import com.cc.library.domain.ReaderType;
import com.cc.library.service.AdminService;
import com.cc.library.service.BackService;
import com.cc.library.service.BookService;
import com.cc.library.service.BookTypeService;
import com.cc.library.service.CollectionService;
import com.cc.library.service.ForfeitService;
import com.cc.library.service.ReaderService;
import com.cc.library.util.Md5Utils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

@SuppressWarnings("serial")
public class ReaderAction extends ActionSupport{
	private ReaderService readerService;
	private BackService backService;
	private AdminService adminService;
	private CollectionService collectionService;
	
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public void setBackService(BackService backService) {
		this.backService = backService;
	}

	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}
	private BookService bookService;
	private BookTypeService bookTypeService;



	public void setCollectionService(CollectionService collectionService) {
		this.collectionService = collectionService;
	}
	public void setBookTypeService(BookTypeService bookTypeService) {
		this.bookTypeService = bookTypeService;
	}
	private ForfeitService forfeitService;

	public void setForfeitService(ForfeitService forfeitService) {
		this.forfeitService = forfeitService;
	}

	private String name;
	private String phone;
	private String email;
	private String oldPwd;
	private String newPwd;
	private String confirmPwd;
	private String userId;
	public Result result;
	private int p;
	private int size;
	private Integer readerType;
	private String paperNO;
	private Integer bookTypeId;	//图书类型
	private String bookName;	//图书名称
	private String autho;	//作者名称
	private String press;	//出版社
	private int bookId;//图书编号
	private String ISBN;
	private int collectionId;
	private int  readerId;






	public void setCollectionId(int collectionId) {
		this.collectionId = collectionId;
	}
	public void setReaderId(int readerId) {
		this.readerId = readerId;
	}
	public void setReaderType(Integer readerType) {
		this.readerType = readerType;
	}

	public void setISBN(String iSBN) {
		ISBN = iSBN;
	}


	public void setBookTypeId(Integer bookTypeID) {
		this.bookTypeId = bookTypeID;
	}


	public void setBookName(String bookName) {
		this.bookName = bookName;
	}


	public void setAutho(String autho) {
		this.autho = autho;
	}


	public void setPress(String press) {
		this.press = press;
	}




	public void setPaperNO(String paperNO) {
		this.paperNO = paperNO;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}







	public Result getResult() {
		return result;
	}




	public void setP(int p) {
		this.p = p;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public ReaderService getReaderService() {
		return readerService;
	}
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public void setName(String name) {
		this.name = name;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}


	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}


	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}


	public void setConfirmPwd(String confirmPwd) {
		this.confirmPwd = confirmPwd;
	}


	/**
	 * 读者个人资料
	 * @return
	 */
	public String getReaderByUserId(){
		Reader reader = new Reader();
		reader.setUserId(userId);

		Reader newReader = readerService.getReaderByUserId(reader);
		if(newReader==null){
			//用户名不存在
			result = new Result(null,"用户不存在",false);
			return SUCCESS;
		}


		newReader.setBorrowInfos(null);
		newReader.setAdmin(null);
		newReader.setPwd(null);
		result=new Result(newReader, "获取成功", true);

		return SUCCESS;
	}
	/**
	 * 根据页码查询图书
	 * @return
	 */
	public String findBookByPage(){
		//获取页面传递过来的当前页码数
		if(p==0){
			p = 1;
		}
		//给pageSize,每页的记录数赋值
		if(size==0){
			size=20;
		}

		PageBean<Book> pb = bookService.findBookByPage(p,size);

		result=new Result(pb,"获取成功",true);
		//存入request域中

		return  "success";
	}



	/**
	 * 读者密码修改
	 * @return
	 */
	public String readerPwd(){
		Reader reader = (Reader) ServletActionContext.getContext().getSession().get("reader");
		int state = -1;//原密码错误
		//取出原密码进行比对
		if(reader.getPwd().equals(Md5Utils.md5(oldPwd))){
			if(newPwd.equals(confirmPwd)){
				state = 1;//修改成功
				reader.setPwd(Md5Utils.md5(newPwd));
				reader = readerService.updateReaderInfo(reader);
				//重新存入session
				ServletActionContext.getContext().getSession().put("reader", reader);
			}else{
				state = 0;//确认密码不一致
			}
		}
		try {
			ServletActionContext.getResponse().getWriter().print(state);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e.getMessage());
		}
		return null;
	}


	/**
	 * 得到指定图书编号的图书信息
	 * ajax请求该方法
	 * 返回该图书信息的json对象
	 * @return
	 */
	public String getBook(){

		Book book = new Book();
		book.setBookId(bookId);
		Book newBook = bookService.getBookById(book);//得到图书

		result=new Result(newBook,"获取成功",true);
		return SUCCESS;
	}
	/**
	 * 得到图书类型的集合
	 * ajax请求该方法
	 * 返回图书类型集合的json对象
	 * @return
	 */
	public String getAllBookTypes(){

		List<BookType> allBookTypes = bookTypeService.getAllBookTypes();



		result=new Result(allBookTypes, "获取成功", true);


		return SUCCESS;
	}
	/*
	 **
	 * 多条件查询图书
	 * @return
	 */
	public String queryBook(){
		//获取页面传递过来的当前页码数
		if(p==0){
			p = 1;
		}
		//给pageSize,每页的记录数赋值
		if(size==0){
			size=20;
		}

		PageBean<Book> pb = null;

		/*	if("".equals(ISBN.trim()) && "".equals(bookName.trim()) && bookTypeId==-1 && "".equals(press.trim()) && "".equals(autho.trim())){
			pb = bookService.findBookByPage(pageCode,pageSize);
		}else{*/
		BookType bookType = new BookType();
		bookType.setTypeId(bookTypeId);
		Book book = new Book(ISBN, bookType, bookName, autho, press);

		pb = bookService.queryBook(book,p,size);
		//}
		if(pb!=null){
			pb.setUrl("queryBook.action?ISBN="+ISBN+"&bookName="+bookName+"&bookTypeId="+bookTypeId+"&press="+press+"&autho="+autho+"&");
		}
		result=new Result(pb,"获取成功",true);
		//ServletActionContext.getRequest().setAttribute("pb", pb);
		return SUCCESS;
	}
	/**
	 * 查询借阅记录
	 * @return
	 */
	public String findMyBorrowInfoByPage(){

		if(p==0){
			p = 1;
		}
		if(size==0){
			size=20;
		}

		Reader reader = new Reader();
		reader.setUserId(userId);

		Reader newReader = readerService.getReaderByUserId(reader);
		if(newReader==null){
			//用户名不存在
			result = new Result(null,"用户不存在",false);
			return SUCCESS;
		}
		//给pageSize,每页的记录数赋值

		PageBean<BackInfo> pb = null;
		pb = backService.findMyBorrowInfoByPage(newReader,p,size);
		result=new Result(pb,"获取成功",true);
		return SUCCESS;
	}
	/**
	 * 查询逾期信息
	 * @return
	 */
	public String findMyForfeitInfoByPage(){
		if(p==0){
			p = 1;
		}
		if(size==0){
			size=20;
		}

		Reader reader = new Reader();
		reader.setUserId(userId);

		Reader newReader = readerService.getReaderByUserId(reader);
		if(newReader==null){
			//用户名不存在
			result = new Result(null,"用户不存在",false);
			return SUCCESS;
		}
		PageBean<ForfeitInfo> pb = null;
		pb = forfeitService.findMyForfeitInfoByPage(newReader,p,size);

		result=new Result(pb,"获取成功",true);
		return SUCCESS;
	}
	/**
	 * 添加收藏
	 * @return
	 */
	public String addCollection(){
		Collection collection =new Collection();
		Reader reader=new Reader();
		reader.setReaderId(readerId);
		Book book=new Book();
		book.setBookId(bookId);
		collection.setBook(book);
		collection.setReader(reader);
		collection.setCreateDate(new Date());
		int r=collectionService.addCollection(collection);
		if(r==0){
			result=new Result(null,"添加失败",false);
		}else{
			result=new Result(null,"添加成功",true);
		}
		
		//增加图书收藏数
		Book newBook=bookService.getBookById(book);
		newBook.setCollectionCount(newBook.getCollectionCount()+1);
		bookService.updateBookInfo(newBook);
		//增加读书收藏数
		Reader newReader=readerService.getReaderById(reader);
		newReader.setCollectCount(newReader.getCollectCount()+1);
		readerService.updateReaderInfo(newReader);
		return SUCCESS;
	}
	
	/**
	 * 删除收藏
	 * @return
	 */
	public String deleteCollectionById(){
		Collection collection =collectionService.findCollectionById(collectionId);
		boolean r=collectionService.deleteCollection(collection);
		if(!r){
			result=new Result(null,"删除失败",false);
		}else{
			result=new Result(null,"删除成功",true);
		}
		
		
		//增加图书收藏数
		Book newBook=collection.getBook();
		newBook.setCollectionCount(newBook.getCollectionCount()-1);
		bookService.updateBookInfo(newBook);
		//增加读书收藏数
		Reader newReader=collection.getReader();
		newReader.setCollectCount(newReader.getCollectCount()-1);
		readerService.updateReaderInfo(newReader);
		return SUCCESS;
	}
	
	/**
	 * 删除收藏
	 * @return
	 */
	public String deleteCollectionByBookAndReader(){
		Collection collection =new Collection();
		Reader reader=new Reader();
		reader.setReaderId(readerId);
		Book book=new Book();
		book.setBookId(bookId);
		collection.setBook(book);
		collection.setReader(reader);
		Collection newCollection =collectionService.findCollectionByBookAndReader(collection);
		if(newCollection!=null){
		boolean r=collectionService.deleteCollection(newCollection);
		if(!r){
			result=new Result(null,"删除失败",false);
		}else{
			result=new Result(null,"删除成功",true);
		}
		}else{
			result=new Result(null,"收藏不存在",false);
		}
		return SUCCESS;
	}
	
	public String getCollectionsByReader(){
		Collection collection =new Collection();
		Reader reader=new Reader();
		reader.setReaderId(readerId);
		collection.setReader(reader);
		//获取页面传递过来的当前页码数
		if(p==0){
			p = 1;
		}
		//给pageSize,每页的记录数赋值
		if(size==0){
			size=20;
		}
		PageBean<Collection> pb=collectionService.getCollectionsByReader(collection, p, size);
		result=new Result(pb,"获取成功",true);
		return SUCCESS;
	}
	public String getCollectionsByBook(){
		Collection collection =new Collection();
		Book book=new Book();
		book.setBookId(bookId);
		collection.setBook(book);
		//获取页面传递过来的当前页码数
		if(p==0){
			p = 1;
		}
		//给pageSize,每页的记录数赋值
		if(size==0){
			size=20;
		}
		PageBean<Collection> pb=collectionService.getCollectionsByBook(collection, p, size);
		result=new Result(pb,"获取成功",true);
		return SUCCESS;
	}

	/**
	 *添加读者
	 * @return
	 */
	public String addReader(){
		//得到当前时间
		Date createTime = new Date(System.currentTimeMillis());
		//得到当前管理员
		Admin admin = new  Admin();
		admin.setAid(1);
	//	Admin newAdmin = adminService.getAdminByUserName(admin);
		//得到当前读者类型
		ReaderType type = new ReaderType();

		type.setReaderTypeId(readerType);
		Reader reader = new Reader(name, Md5Utils.md5("123456"), phone, type, email, admin, paperNO, createTime);
		reader.setUserId(userId);
		Reader oldReader = readerService.getReaderByUserId(reader);//检查是否已经存在该paperNO的读者

		if(oldReader!=null){
			result=new Result(-1,"已经存在",false);
		}else{


			boolean b = readerService.addReader(reader);
			if(b){
				result=new Result(null,"添加成功",true);
			}else{
				result=new Result(-2,"添加失败",false);
			}
		}

		return SUCCESS;

	}
	
	
	
}
